<template>
	<form-container
		:title="formData.id ? '编辑模块' : '增加模块'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="仓库编号：" name="warehousecode">
				<a-input v-model:value="formData.warehousecode" placeholder="请输入仓库编号" allow-clear />
			</a-form-item>
      <a-form-item label="仓库名称：" name="warehousename">
				<a-input v-model:value="formData.warehousename" placeholder="请输入仓库名称" allow-clear />
			</a-form-item>
      <a-form-item label="负责人" name="person">
				<a-input v-model:value="formData.person" placeholder="请输入负责人" allow-clear />
			</a-form-item>
      <a-form-item label="面积" name="area">
				<a-input v-model:value="formData.area" placeholder="请输入面积" allow-clear />
			</a-form-item>
      <a-form-item label="地址" name="address">
				<a-input v-model:value="formData.address" placeholder="请输入地址" allow-clear />
			</a-form-item>
      <a-form-item label="仓库类型：" name="warehousetype">
				<a-input v-model:value="formData.warehousetype" placeholder="请输入仓库类型" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-checkbox class="mr-2" v-if="!formData.id" v-model:checked="addNext">继续新增下一条</a-checkbox>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
	</form-container>
</template>

<script setup>
	import { required } from '@/utils/formRules'
	import moduleApi from '@/api/sys/resource/moduleApi'
   import warehouseapi from '@/api/wms/warehouse/warehouseapi'
	// 默认是关闭状态
	let visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const treeData = ref([])

	// 表单数据
	const formData = ref({})
	// 继续新增下一条
	const addNext = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		addNext.value = false
		formData.value = {
			sortCode: 99
		}
		if (record) {
			formData.value = Object.assign({}, record)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}


	// 默认要校验的
	const formRules = {
		warehousecode: [required('请输入仓库编号')],
		warehousename: [required('请输入仓库名称')],
		person: [required('请输入负责人')],
    area: [required('请输入面积')],
    address: [required('请输入地址')],
    warehousetype: [required('请输入仓库类型')],
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			warehouseapi.Add(formData.value).then(() => {
				if (addNext.value) {
					formData.value = {
						sortCode: 99
					}
				} else {
					onClose()
				}
				emit('successful')
			})
		})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
